package com.t.bricks.bussiness.service.coupon.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.t.bricks.bussiness.db.dao.coupon.CouponDao;
import com.t.bricks.bussiness.db.entity.coupon.Coupon;
import com.t.bricks.bussiness.model.coupon.AddInfo;
import com.t.bricks.bussiness.model.coupon.ConditionCoupon;
import com.t.bricks.bussiness.service.coupon.CouponService;
import com.t.bricks.model.MsgEmity;
import com.t.bricks.model.OrderInfoList;
import com.t.bricks.model.Page;
import com.t.bricks.utils.function.data.MoneyUtil;
import com.t.bricks.utils.function.json.JacksonShell;
import com.t.bricks.utils.spring.ModuleUtil;

/**
 * 优惠卷'Coupon表'基本业务操作类接口实现
 */
@Service
public class CouponServiceImpl implements CouponService {

	@Autowired
	private CouponDao couponDao;

	/**
	 * 批量添加
	 * @param addInfo 批量添加信息实体
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity adds(AddInfo addInfo) {
		if (null == addInfo) {
			return MsgEmity.err(8011, "批量添加信息实体不能为空");
		}
		
		if (null == addInfo.getiSize() || addInfo.getiSize().intValue() < 1) {
			return MsgEmity.err(8201, "添加数量必须大于0");
		}
		
		Coupon coupon = JacksonShell.toJavaObject(addInfo, Coupon.class);
		if (null == coupon) {
			return MsgEmity.err(8202, "对象转换失败");
		}

		coupon.setuId("随便");//随便设置一个值
		coupon.setsNo("随便");//随便设置一个值
		
		MsgEmity me = ModuleUtil.validDbEntityByAdd(coupon);// 通用的基本数据验证,如果要进行比较复杂的数据验证则可以屏蔽掉此方法自行编写
		if (!me.isSuccess()) {
			return me;
		}
		
		if (!MoneyUtil.isPlus(coupon.getmPrice())) {
			return MsgEmity.err(8203, "金额必须是正数");
		}
		
		if (MoneyUtil.isNegative(coupon.getmMinMoney())) {
			return MsgEmity.err(8204, "'最小金额限制'不能是负数");
		}
		
		if (coupon.getiMaxByCustomer().intValue() < 1) {
			return MsgEmity.err(8205, "'限制领取数'必须大于0");
		}
		
		if (coupon.getdStartDate().after(coupon.getdEndDate())) {
			return MsgEmity.err(8206, "'开始时间'必须小于'过期时间'");
		}
		
		me = ModuleUtil.supplyDbEntityAttrByAdd(coupon, "随便", null);// 补充数据,如添加id、当前用户、当前时间等
		if (!me.isSuccess()) {
			return me;
		}
		
		return couponDao.adds(coupon, addInfo.getiSize());
	}

	/**
	 * 删除
	 * @param uId 记录编号
	 * @param iVersion 数据版本号
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity del(String uId, Integer iVersion) {
		if (null == uId || "".equals(uId.trim())) {
			return new MsgEmity(false, "信息编号参数错误，请刷新重试！", 8001);
		}
		
		if (null == iVersion || iVersion.intValue() < 1) {
			return new MsgEmity(false, "信息版本参数错误，其值应大于0！", 8002);
		}

		return couponDao.del(uId, iVersion);
	}

	/**
	 * 根据记录编号取对象
	 * @param uId
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity findById(String uId) {
		if (null == uId || "".equals(uId.trim())) {
			return new MsgEmity(false, "编号不能为空！", 8001);
		}
		
		return couponDao.findById(uId);
	}
	
	/**
	 * 根据字段名取指定记录编号的数据库表中对应字段的值
	 * @param uId 记录编号
	 * @param fieldNames 待取数据的字段名称集合
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity getValueByFieldName(String uId, List<String> fieldNames) {
		if (null == uId || "".equals(uId.trim())) {
			return new MsgEmity(false, "编号不能为空！", 8001);
		}
		
		if (null == fieldNames || fieldNames.size() < 1) {
			return new MsgEmity(false, "待查询字段不能为空！", 8002);
		}
		
		return couponDao.getValueByFieldName(uId, fieldNames);
	}
	
	/**
	 * 查询分页
	 * @param page 分页信息对象
	 * @param orders 排序数据集合
	 * @param sLikeStr 全文检索
	 * @param condition 查询条件对象
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity findByPage(Page page, OrderInfoList orders, String sLikeStr,
			ConditionCoupon condition) {
		if (null == page) {
			page = new Page(new ArrayList<Coupon>());
		}

		return couponDao.findByPage(page, orders, sLikeStr, condition);
	}

	/**
	 * 根据记录编号查询符合分页数据的某条记录
	 * @param uId
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity findByPageRow(String uId) {
		if (null == uId || "".equals(uId.trim())) {
			return new MsgEmity(false, "编号不能为空！", 8001);
		}
		
		return couponDao.findByPageRow(uId);
	}

	/**
	 * 创建Excel文件内容
	 * @param orders 排序数据集合
	 * @param sLikeStr 全文检索
	 * @param condition 查询条件对象
	 * @return MsgEmity 返回结构中data为创建的经过压缩的Excel内容,接收端需要进行解压,msg为文件名
	 */
	@Override
	public MsgEmity export(OrderInfoList orders, String sLikeStr, ConditionCoupon condition) {
		MsgEmity me = couponDao.findByExport(orders, sLikeStr, condition);//查询数据
		if(!me.isSuccess()) {
			return me;
		}
		
		return ModuleUtil.crateExcel(me.getData(), "优惠卷.xls");//根据Excel的xml模板创建文件内容
	}
	
	//----------------------------- 自动生成代码结束 ---------------------------//

	/**
	 * 批量删除数据
	 * 注:只能删除未被领取的数据
	 * @param sType 分类
	 * @param sWaresType 限制商品分类
	 * @return int
	 */
	@Override
	public MsgEmity dels(String sType, String sWaresType) {
		if (null == sType || "".equals(sType.trim())) {
			sType = null;
		} else {
			sType = sType.trim();
		}

		if (null == sWaresType || "".equals(sWaresType.trim())) {
			sWaresType = null;
		} else {
			sWaresType = sWaresType.trim();
		}
		
		return couponDao.dels(sType, sWaresType);
	}

	/**
	 * 占用(一张)
	 * @param sType 分类
	 * @param mPrice 金额
	 * @return
	 */
	@Override
	public MsgEmity employ(String sType, BigDecimal mPrice) {
		if (null == sType || "".equals(sType.trim())) {
			return new MsgEmity(false, "分类不能为空！", 8001);
		}

		if (!MoneyUtil.isPlus(mPrice)) {
			return new MsgEmity(false, "金额必须是正数！", 8001);
		}
		
		return couponDao.employ(sType, mPrice, ModuleUtil.currentLoginUserId());
	}

	/**
	 * 按编号占用
	 * @param uId 记录编号
	 * @param iVersion 当前版本号
	 * @return
	 */
	@Override
	public MsgEmity employById(String uId, Integer iVersion) {
		if (null == uId || "".equals(uId.trim())) {
			return new MsgEmity(false, "信息编号参数错误，请刷新重试！", 8001);
		}
		
		if (null == iVersion || iVersion.intValue() < 1) {
			return new MsgEmity(false, "信息版本参数错误，其值应大于0！", 8002);
		}

		return couponDao.employById(uId, ModuleUtil.currentLoginUserId(), iVersion);
	}
	
	/**
	 * 将过期数据更新状态
	 * @return
	 */
	@Override
	public MsgEmity expire() {
		return couponDao.expire();
	}
	
	/**
	 * 查询当前顾客所持优惠卷
	 * @param sType
	 * @param mPrice
	 * @param iState
	 * @return MsgEmity List<Coupon>
	 */
	@Override
	public MsgEmity findByCustomer(String sType, BigDecimal mPrice, Integer iState) {
		if (null != mPrice && !MoneyUtil.isPlus(mPrice)) {
			return new MsgEmity(false, "金额参数必须是正数！", 8001);
		}
		
		if (null != iState && iState.intValue() < 2) {
			return new MsgEmity(false, "状态参数应大于1！", 8002);
		}
		
		return couponDao.findByCustomer(sType, mPrice, iState, ModuleUtil.currentLoginUserId());
	}

}
